package com.example.danwei;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity  {
    int proes = 0; //进度条进度值
    ProgressBar bar1;
    Button but1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bar1 = findViewById(R.id.pro1);
        but1 = findViewById(R.id.but1);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        proes++; //增加进度值
                        if(proes==100){
                            timer.cancel(); //当proes到最大值时停止增加
                        }else{
                            bar1.setProgress(proes); // proes当前进度
                        }
                    }
                });
            }
        },100,100); //间隔时间（单位为毫秒）
    }

}

